﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using YXQ.Common.Helper;
using YXQ.Common.Meta;
using YXQ.Common.Extension;
namespace YXQ.Dao
{
    public class OperatingDataDao
    {

        public bool DeleteData(int id, string tableName)
        {

            List<Parameter> parames = new List<Parameter>() { 
                new Parameter(){
                    DType=DbType.Int32,
                    Value=id.ToString(),
                    ValuePrm="id"
                }
            };

            string sql = "update " + tableName + " set isdelete=1 where id=@id";
            try
            {
                int obj = DBHelper.ExecuteNonQuery(sql, parames);
                if (obj > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                LogHelper.Error("执行SQL报错,出错SQL=" + sql + ";" + ex.Message);
                return false;
            }
        }



        /// <summary>
        /// 通用查询单条数据返回实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="id">ID</param>
        /// <param name="tableName">数据库表名</param>
        /// <returns>返回数据</returns>
        public T SelectData<T>(int id, string tableName)
        {
            List<Parameter> parames = new List<Parameter>();
            Parameter ID = new Parameter();
            ID.DType = DbType.Int32;
            ID.Value = id.ToString();
            ID.ValuePrm = "id";
            parames.Add(ID);
            string sql = "select * from " + tableName + " where id=@id";
            try
            {
                DataTable dt = DBHelper.ExecuteDataSet(sql, parames);
                if (dt != null && dt.Rows.Count > 0)
                {
                    return dt.ToEntity<T>();
                }
                return default(T);

            }
            catch (Exception ex)
            {
                LogHelper.Error("执行SQL报错,出错SQL=" + sql + ";" + ex.Message);
                return default(T);
            }
        }



        public int ExecuteNonQuery(string strSQL)
        {
            if (!string.IsNullOrEmpty(strSQL))
            {
                try
                {
                    return DBHelper.ExecuteNonQuery(strSQL);
                }
                catch (Exception ex)
                {
                    LogHelper.Error("执行SQL报错,出错SQL=" + strSQL + ";" + ex.Message);

                    return 0;
                }
            }
            else
            {
                LogHelper.Error("没有获取到SQL:" + strSQL);
                return 0;
            }
        }



        public DataTable GetCommonData(string tableName)
        {
            if (!string.IsNullOrEmpty(tableName))
            {
                string sql = "SELECT * FROM " + tableName + " where IsDelete=0";
                try
                {
                    DataTable dt = DBHelper.ExecuteDataSet(sql);
                    return dt;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("执行SQL报错,出错SQL=" + sql + ";" + ex.Message);

                    return null;
                }
            }
            else
            {
                LogHelper.Error("没有获取装载表:" + tableName);
                return null;
            }
        }


        public DataTable GetCommonData(string tableName, int rows, int page, string orderString, string isdel, out int total)
        {
            total = 0;
            string strWhr = " WHERE 1=1  ";

            if (!string.IsNullOrEmpty(tableName))
            {
                if (!string.IsNullOrEmpty(isdel))
                {
                    strWhr += " AND ( IsDelete is null or IsDelete=" + isdel + " ) ";
                }

                string sql = "SELECT * FROM " + tableName + strWhr + orderString;
                try
                {
                    DataTable dt = DBHelper.GetPageDataTable(sql.ToString(), ++page, rows, out total);
                    return dt;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("执行SQL报错,出错SQL=" + sql + ";" + ex.Message);

                    return null;
                }
            }
            else
            {
                LogHelper.Error("没有获取装载表:" + tableName);
                return null;
            }
        }



        public DataTable GetCommonData(string tableName, int rows, int page, string orderString, out int total)
        {
            total = 0;
            string strWhr = " WHERE 1=1  ";

            if (!string.IsNullOrEmpty(tableName))
            {

                string sql = "SELECT * FROM " + tableName + strWhr + orderString;
                try
                {
                    DataTable dt = DBHelper.GetPageDataTable(sql.ToString(), ++page, rows, out total);
                    return dt;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("执行SQL报错,出错SQL=" + sql + ";" + ex.Message);

                    return null;
                }
            }
            else
            {
                LogHelper.Error("没有获取装载表:" + tableName);
                return null;
            }
        }


        /// <summary>
        /// 获取一条数据，如果存在多条则只返回第一条数据
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="where">过滤条件</param>
        /// <returns></returns>
        public DataTable GetSingleData(string tableName, string where)
        {
            if (!string.IsNullOrEmpty(tableName))
            {
                string sql = "SELECT top 1 * FROM " + tableName + " where IsDelete=0" + where;
                try
                {
                    DataTable dt = DBHelper.ExecuteDataSet(sql);
                    return dt;
                }
                catch (Exception ex)
                {
                    LogHelper.Error("执行SQL报错,出错SQL=" + sql + ";" + ex.Message);

                    return null;
                }
            }
            else
            {
                LogHelper.Error("没有获取装载表:" + tableName);
                return null;
            }
        }
        public int DataIsDistanct(string tableName, int id, string codeName, string codeValue, string isDelete)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("SELECT COUNT(0) FROM {0} WHERE {1} ='{2}' ", tableName, codeName, codeValue);
            if (id > 0)
                sb.AppendFormat(" AND id <> {0} ", id);
            if (isDelete == "1")
                sb.AppendFormat(" AND isdelete is null || isdelete == 0 ");
            try
            {
                return int.Parse(DBHelper.ExecuteScalar(sb.ToString()).ToString());
            }
            catch (Exception)
            {
                LogHelper.Error("没有获取到SQL:" + sb.ToString());
                return -1;
            }

        }


    }
}
